package force;

import java.awt.event.KeyEvent;
import java.util.Scanner;
import object.mass.Mass;
import sim.Simulation;


/**
 * A class that implements a source of viscosity.
 * @author Niel Lebeck and Wynne Lok
 *
 */
public class Viscosity extends GlobalForceSource {
    private static final double DEFAULT_VISCOSITY_SCALE = 0.01;
    private static final int REVERSE_ANGLE_OFFSET = 180;

    private boolean myViscosityOn;
    private double myViscosityScale;
    
    /**
     * 
     * @param line String
     */
    public Viscosity(String line) {
        Scanner s = new Scanner(line);
        String type = s.next();
        myViscosityScale = s.nextDouble();
        myViscosityOn = true;
    }
    
    /**
     * 
     */
    public Viscosity() {
        myViscosityScale = DEFAULT_VISCOSITY_SCALE;
        myViscosityOn = false;
    }
    
    /**
     * Calculate the force of viscosity on the given Mass.
     * @param m Mass
     * @param sim Simulation
     * @return
     */
    @Override
    public Force getForce (Mass m, Simulation sim) {
        Force result;
        if (myViscosityOn) {
            double viscosityDir = m.getVelocity().getDirection()
                    + REVERSE_ANGLE_OFFSET;
            double viscosityMag = m.getVelocity().getMagnitude()
                    * myViscosityScale;
            result = new Force(viscosityDir, viscosityMag);
        }
        else {
            result = new Force(0, 0);
        }
        return result;
    }

    @Override
    public void toggle(int keycode) {
        if (keycode == KeyEvent.VK_V) {
            myViscosityOn = !myViscosityOn;
        }
    }

}
